package Service;

import Domain.House;

import java.util.Arrays;

/*
  定义House[],保存House对象
* 1.响应HouseView的调用
* 2.完成对房屋信息的增删改查
*
* */
public class HouseService {

    private House houses[];
    private int count = 1;

    public HouseService(int size) {
        houses = new House[size];//当创建HouseService对象，指定数组大小
        houses[0] = new House(1, "jack", "18638", "二七区", 2000, "未出租");
    }

    //add方法，用于添加房源时新增对象
    public boolean add(House newHouse) {
        if (count == houses.length) {
            System.out.println("房屋已满，无法添加!");
            return false;
        }
        houses[count++] = newHouse;
        return true;

    }

    public House search(int delId){

        for (int i = 0; i < count; i++) {
            if (delId == houses[i].getId()) {
                return houses[i];
            }
        }

        return null;
    }

    //del方法，用于删除房屋信息
    public boolean del(int delId) {
        int index = -1;

        for (int i = 0; i < count; i++) {
            if (delId == houses[i].getId()) {
                index = i;
            }
        }

        if (index == -1) {
            System.out.println("查找id失败!");
            return false;
        }

        for (int i = index; i < count - 1; i++) {
            houses[i] = houses[i + 1];
        }
        houses[--count] = null;
        return true;
    }

    //list方法，返回houses数组
    public House[] list() {
        return houses;
    }



    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
